package com.sui.bigdata.rtcadmin.configuration;

import com.sui.bigdata.rtcadmin.util.CheckUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * @author YongChen
 * @date 2020/8/12 13:53
 * @description
 * @email yong_chen@sui.com
 */

@Aspect
@Configuration
public class TokenAspect {

    @Autowired
    private CheckUtils checkUtils;

    @Pointcut("execution(* com.sui.bigdata.rtcadmin.controller.*.*(..)) " +
            "&& !execution(* com.sui.bigdata.rtcadmin.controller.HealthController.*(..)) " +
            "&& !execution(* com.sui.bigdata.rtcadmin.controller.LoginController.*(..)) ")
    public void chekcToken() {
    }

    @Before("chekcToken()")
    public void doBefore() throws Exception {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        Enumeration<String> authorization = sra.getRequest().getHeaders("Authorization");
        checkUtils.chekcToken(authorization);
    }
}
